package com.newjiabo.web.controller.ski;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.newjiabo.common.annotation.Excels;
import com.newjiabo.common.annotation.RepeatSubmit;
import com.newjiabo.common.core.enums.ResultCode;
import com.newjiabo.common.exception.ServiceException;
import com.newjiabo.ski.domain.SkiSetMealServicesRel;
import com.newjiabo.ski.domain.dto.SetMealServicesRelDto;
import com.newjiabo.ski.service.ISkiSetMealServicesRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.newjiabo.common.annotation.Log;
import com.newjiabo.common.core.controller.BaseController;
import com.newjiabo.common.core.domain.AjaxResult;
import com.newjiabo.common.enums.BusinessType;
import com.newjiabo.ski.domain.SkiSetMeal;
import com.newjiabo.ski.service.ISkiSetMealService;
import com.newjiabo.common.utils.poi.ExcelUtil;
import com.newjiabo.common.core.page.TableDataInfo;

/**
 * 套餐配置Controller
 *
 * @author CaoMengHui
 * @date 2023-09-18
 */
@RestController
@RequestMapping("/system/meal")
@Api(tags = "后台;套餐配置")
public class SkiSetMealController extends BaseController {
    @Resource
    private ISkiSetMealService skiSetMealService;
    @Resource
    private ISkiSetMealServicesRelService skiSetMealServicesRelService;

    /**
     * 查询套餐配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:meal:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询套餐配置列表")
    public TableDataInfo list(SkiSetMeal skiSetMeal) {
        startPage();
        List<SkiSetMeal> list = skiSetMealService.selectSkiSetMealList(skiSetMeal);
        return getDataTable(list);
    }

    /**
     * 获取所有的数据
     *
     * @return
     * @author CaoMengHui.
     * @date 2023/9/20 9:31
     */
    @ApiOperation("get all data")
    @GetMapping("/all")
    public TableDataInfo all() {
        return getDataTable(skiSetMealService.selectAll());
    }

    /**
     * 导出套餐配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:meal:export')")
    @Log(title = "导出套餐配置列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出套餐配置列表")
    public void export(HttpServletResponse response, SkiSetMeal skiSetMeal) {
        List<SkiSetMeal> list = skiSetMealService.selectSkiSetMealList(skiSetMeal);
        ExcelUtil<SkiSetMeal> util = new ExcelUtil<SkiSetMeal>(SkiSetMeal.class);
        util.exportExcel(response, list, "套餐配置数据");
    }

    /**
     * 获取套餐配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:meal:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取套餐配置详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(skiSetMealService.getInfo(id));
    }

    /**
     * 新增套餐配置
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:meal:add')")
    @Log(title = "新增套餐配置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增套餐配置")
    public AjaxResult add(@RequestBody @Validated SetMealServicesRelDto setMealServicesRelDto) {
        return toAjax(skiSetMealService.addSetMeal(setMealServicesRelDto));
    }

    /**
     * 修改套餐配置
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:meal:edit')")
    @Log(title = "修改套餐配置", businessType = BusinessType.UPDATE)
    @PutMapping("/{setMealId}")
    @ApiOperation(value = "修改套餐配置")
    public AjaxResult edit(@PathVariable("setMealId") String setMealId , @RequestBody @Validated SetMealServicesRelDto setMealServicesRelDto) {
        return toAjax(skiSetMealService.updateSetMeal(setMealServicesRelDto,setMealId));
    }

    /**
     * 删除套餐配置
     */
    @PreAuthorize("@ss.hasPermi('system:meal:remove')")
    @Log(title = "删除套餐配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除套餐配置")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(skiSetMealService.deleteSkiSetMealByIds(ids));
    }

    @ApiOperation(value = "绑定服务")
    @PostMapping(value = "/services/bind")
    public AjaxResult bindingServices(@RequestBody List<SkiSetMealServicesRel> skiSetMealServicesRelList) {
        // 初始化
        if (CollectionUtil.isEmpty(skiSetMealServicesRelList)) {
            throw new ServiceException(ResultCode.REQUEST_PARAMETER_CANNOT_EMPTY);
        }
        for (SkiSetMealServicesRel skiSetMealServicesRel : skiSetMealServicesRelList) {
            skiSetMealServicesRel.setCreateBy(getUsername());
        }
        return toAjax(skiSetMealServicesRelService.bindingServices(skiSetMealServicesRelList));
    }
}
